#include "Model.h"
#include "../../OpenGL.h"
#include "../Str.h"
#include <vector>
#include "../Img.h"

class _ModelMD2 : public _ModelDef
{
public:
	bool load(const char* filename);
	int getFrameCount();
	void draw(int &frame,double scale);
	bool hasFrames(){return true;}
	
private:
	struct MD2Header
	{
		int _id;         // File Type - Normally 'IPD2'
		int version;    
		int texWidth;   // Texture width
		int texHeight;  // Texture height 
		int frameSize;  // Size for frames in bytes
		int nTextures;  // Number of textures
		int nVertices;  // Number of vertices
		int nTexCoords; // Number of UVs
		int nTriangles; // Number of polys
		int nGLCmd;     // Number of GL Commmands
		int nFrames;    // Number of frames
		int texOffset;  // Offset to texture name(s)
		int uvOffset;   // Offset to UV data
		int faceOffset; // Offset to poly data
		int frameOffset;// Offset to first frame  
		int glCmdOffset;// Offset to GL Cmds          
		int eofOffset;  // Size of file
	};
	
	struct MD2FrameInfo
	{
		float scale[3];
		float translate[3];
		char name[16];
	};
	
	struct MD2Face
	{
		short p1,p2,p3;
		short uv1,uv2,uv3;
	};
	struct MD2Vtx
	{
		unsigned char vtx[3];
		unsigned char lNorm;
	};
	struct Mesh_Vtx{float x,y,z;};
	struct Mesh_UV{float u,v;};
	struct MD2Frame
	{
		std::vector<Mesh_Vtx> vtx;
		std::vector<Mesh_Vtx> norm;
	};
	
	struct MD2TexCoord{short u,v;};
	
	std::vector<MD2Face> face;
	std::vector<MD2Frame> frame;
	std::vector<Mesh_UV> uv;
	Img *tex;
	
	void CalcNormal(Mesh_Vtx v1,Mesh_Vtx v2,Mesh_Vtx v3,Mesh_Vtx* Result);
	void clear();
};